import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.*;
import javax.swing.plaf.PanelUI;


public class ClientSocket {

	private String _ip;
	private int _port;
	private Socket _socket;
	private PrintWriter _out;
	private BufferedReader _in;
	
	public ClientSocket(String ip,int port)
	{	
		_ip = ip;
		_port = port;
		
		EstablishConnection();
	}
	
	private void EstablishConnection()
	{
		try {
			_socket = new Socket(_ip, _port);
		} catch (UnknownHostException e) {
			System.out.println("adresse introuvable");
		} catch (IOException e) {
			System.out.println("IO exception");
		}
		
		// initialize the PrintWriter
		try {
			_out = new PrintWriter(_socket.getOutputStream(), true);
		} catch (IOException e) {
			System.out.println("PrintWriter init exception");
		}
		
		try {
			_in = new BufferedReader(new InputStreamReader(
                    _socket.getInputStream()));
		} catch (IOException e) {
			System.out.println("PrintWriter init exception");
		}
		
	}

	public void SendString(String text)
	{
		_out.println(text);
	}
	
	public void ReadString()
	{
		try {
			System.out.println("echo: " + _in.readLine());
		} catch (IOException e) {
			System.out.println("ReadString exception");
		}
	}
	
	
}
